﻿@{
    ViewData["Title"] = "OpenXML操作Word";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
    <a href="https://docs.microsoft.com/zh-cn/office/open-xml/structure-of-a-wordprocessingml-document" target="_blank">Open XML</a>
</div>
<div>
    <form>
        <input id="editFile" type="file" style="display:none;" onchange="$('#editFileText').val($(this).val());" />
        <input id="editFileText" type="text" readonly style="width:300px;" />
        <button type="button" onclick="$('#editFile').click();">选择文件</button>
        <button type="button" onclick="uploadWord();">上传</button>
        <a asp-controller="Home" asp-action="ExportOpenXML" asp-route-type="1" target="_blank">简单文本下载</a>
        <a asp-controller="Home" asp-action="ExportOpenXML" asp-route-type="2" target="_blank">简单表格下载</a>
        <a asp-controller="Home" asp-action="ExportOpenXML" asp-route-type="3" target="_blank">插入图片下载</a>
        <a asp-controller="Home" asp-action="ExportOpenXML" asp-route-type="4" target="_blank">书签使用下载</a>
    </form>
</div>

@section Scripts{
    <script type="text/javascript">
        //上传
        function uploadWord() {
            if (!$("#editFile").val()) {
                ShowMessage("请选择文件!");
                return false;
            }

            var formData = new FormData();
            formData.append("file", $("#editFile")[0].files[0]);
            $.ajax({
                url: "@Url.Action("UploadOpenXML")",
                type: 'POST',
                data: formData,
                dataType: "json",
                processData: false,
                contentType: false,
                async: false,
                success: function (rsp) {
                    if (rsp.isOk) {
                        alert("控制台查看！");
                        console.table(rsp.data);
                    } else {
                        alert(rsp.msg);
                    }
                },
                error: function (data) {
                    alert("文件上传失败,请重试");
                }
            });

            return false;//防止跳转
        }
    </script>
}